package com.castle.fortress.admin.pay.controller;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.pay.dto.PayRequest;
import com.castle.fortress.admin.pay.dto.PayResponse;
import com.castle.fortress.admin.pay.dto.RefundRequest;
import com.castle.fortress.admin.pay.dto.RefundResponse;
import com.castle.fortress.admin.pay.enums.PayTypeEnum;
import com.castle.fortress.admin.pay.service.PayService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.CommonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 支付相关
 * @author castle
 */
@Controller
public class PayController {


    @Autowired
    private PayService payService;

    /**
     * 统一下单
     * @return
     */
    @CastleLog(operLocation = "统一下单", operType = OperationTypeEnum.OTHER)
    @ApiOperation("统一下单")
    @PostMapping("/topay/uniorder")
    @ResponseBody
    public RespBody<PayResponse> uniorder(@RequestBody PayRequest request,HttpServletRequest servletRequest) {
        if(CommonUtil.verifyParamNull(request,request.getPayType(),request.getOrderId(),request.getOrderAmount(),request.getSceneCode())){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        String ip = WebUtil.getIpAddress(servletRequest);
        if(StrUtil.isEmpty(ip)){
            ip = "127.0.0.1";
        }
        request.setPayerClientIp(ip);
        return payService.uniorder(request);
    }

    /**
     * 异步回调
     */
    @CastleLog(operLocation = "异步回调", operType = OperationTypeEnum.OTHER)
    @PostMapping(value = "/topay/notify/{payType}/{sceneCode}")
    @ApiOperation("异步回调")
    @ResponseBody
    public String notify(HttpServletRequest request,@RequestBody String notifyData, @PathVariable(value="payType") PayTypeEnum payType, @PathVariable(value="sceneCode") String sceneCode) {
        System.out.println("异步回调-paramterMap:"+ request.getParameterMap());
        System.out.println("异步回调-notifyData:"+ notifyData);
        RespBody<PayResponse> response = payService.asyncNotify(request,notifyData,payType, sceneCode);
        //TODO 依据支付结果处理业务代码
        System.out.println(response);
        return "success";
    }

    /**
     * 退款
     * @param refundRequest
     * @return
     */
    @CastleLog(operLocation = "退款", operType = OperationTypeEnum.OTHER)
    @PostMapping("/topay/refund")
    @ResponseBody
    public RespBody<RefundResponse> refund(@RequestBody RefundRequest refundRequest) {

        return payService.refund(refundRequest);
    }
}
